package com.example.first.activities;


import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.first.R;
import com.example.first.adapters.AddressAdapter;
import com.example.first.modle.AddressBook;
import com.example.first.modle.ApiResponse;
import com.example.first.services.ApiService;
import com.example.first.utils.RetrofitClient;

import java.util.List;

import retrofit2.Call;

public class AddressActivity extends AppCompatActivity {

    private ImageView back;
    private ListView addressList;

    private Button add;

    private AddressAdapter addressAdapter;

    private List<AddressBook> address;



    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.address);

        // 设置 Toolbar 标题
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("外卖订餐");
        }


        // 设置状态栏颜色
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.parseColor("#FFC300")); // 设置为你想要的颜色
        }


        //返回
        back = findViewById(R.id.back);
        back.setOnClickListener(v -> finish());

        add=findViewById(R.id.add);
        add.setOnClickListener(v -> {
           Intent intent = new Intent(AddressActivity.this, AddressEditActivity.class);
           startActivity(intent);
        });

        //获取地址信息
        addressList=findViewById(R.id.list_address);
        getAddressList();
    }

    public void getAddressList(){
        ApiService apiService= RetrofitClient.getApiService();
        Call<ApiResponse<List<AddressBook>> > call=apiService.getAddressList();
        call.enqueue(new retrofit2.Callback<ApiResponse<List<AddressBook>>>() {
            @Override
            public void onResponse(Call<ApiResponse<List<AddressBook>>> call, retrofit2.Response<ApiResponse<List<AddressBook>>> response) {
                if(response.isSuccessful() && response.body()!=null){
                    if(response.body().getCode()==1){
                        address=response.body().getData();
                        addressAdapter=new AddressAdapter(AddressActivity.this,address);
                        addressList.setAdapter(addressAdapter);
                    }
                    else {
                        //失败
                        Toast.makeText(AddressActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(AddressActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<ApiResponse<List<AddressBook>>> call, Throwable t) {
                //处理失败情况
                Toast.makeText(AddressActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
